<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">昵称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nickname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="startTime" id="startTime" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="endTime" id="endTime" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <script type="text/html" id="toolbarAdd">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">添加记录</button>
            </div>
        </script>

        <script type="text/html" id="recTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="recEdit">修改成单数</a>
        </script>

        <script type="text/html" id="empTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="empEdit">修改</a>
        </script>

        <script type="text/html" id="supTableBar">
            {{#  if(d.id > 0){ }}
            <a class="layui-btn layui-btn-xs" lay-event="supEdit">修改</a>
            {{#  } }}
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            roleType = layui.data("user").roleType,
            table = layui.table;

        laydate.render({
            elem: '#startTime',
            type: 'datetime'
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime'
        });

        var cols = [[
            {field: 'nickname', title: '昵称'},
            {field: 'completeDate', title: '日期'},
            {field: 'cluesCount', title: '线索条数'},
            {field: 'addFanCount', title: '加粉数'},
            {field: 'addFanFailCount', title: '加粉未通过数'},
            {field: 'phoneCount', title: '电话沟通数'},
            {field: 'phoneSuccessCount', title: '有效接通数'},
            {field: 'phoneNotWxCount', title: '电话非微信数'},
            {
                field: 'phoneSuccessPercent',
                title: '接通率',
                templet: function (d) {
                    return (d.phoneSuccessCount * 100 / d.cluesCount).toFixed(2) + "%"
                }
            },
            {
                field: 'phoneNotWxCount',
                title: '加粉率',
                templet: function (d) {
                    return (d.addFanCount * 100 / d.cluesCount).toFixed(2) + "%"
                }
            },
            {field: 'orderSuccessCount', title: '成单单数'},
            {
                field: 'orderSuccessPercent',
                title: '转换率',
                templet: function (d) {
                    return (d.orderSuccessCount * 100 / d.cluesCount).toFixed(2) + "%"
                }
            },
            {
                field: 'addFanCountOrderSuccessPercent',
                title: '加粉转换率',
                templet: function (d) {
                    return (d.orderSuccessCount * 100 / d.addFanCount).toFixed(2) + "%"
                }
            },
            {field: 'remark', title: '其他备注'}
        ]]

        if (roleType === "RECORDER") {
            cols[0].push({title: '操作', toolbar: '#recTableBar', align: "center"});
        } else if (roleType === "EMPLOYEE") {
            cols[0].push({title: '操作', toolbar: '#empTableBar', align: "center"});
        } else if (roleType === "SUPERVISOR" || roleType === "ADMIN") {
            // cols[0].splice(cols[0].findIndex(item => item.field === "nickname"), 1);
            cols[0].push({title: '操作', toolbar: '#supTableBar', align: "center"});
        }

        table.render({
            elem: '#currentTableId',
            // url: '../api/user.json',
            url: '/zxjy/statisticsall/list/' + roleType.toLowerCase(),
            toolbar: roleType === "EMPLOYEE" ? '#toolbarAdd' : '',
            method: 'post',
            contentType: 'application/json',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cellMinWidth: 100,
            cols: cols,
            response: {statusCode: 200},
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.state, //解析接口状态
                    "msg": res.success, //解析提示文本
                    "count": res.result.total, //解析数据长度
                    "data": res.result.list //解析数据列表
                };
            },
            request: {
                pageName: 'pageNum' // 页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            limits: [10, 15, 20, 25, 50, 100],
            limit: roleType === "SUPERVISOR" ? 100 : 10,
            page: true,
            skin: 'line',
            done: function (res, curr, count) {{
                var that = this.elem.next();
                res.data.forEach(function (item, index) {
                    if (item.id === "null") {
                        var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                        tr.css("background-color", "yellow");
                    }
                });
            }}
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            //执行搜索重载
            table.reload('currentTableId', {
                url: '/zxjy/statisticsall/list/' + roleType.toLowerCase()
                , where: {
                    nickname: data.field.nickname,
                    brandType: data.field.brandType === '' ? null : data.field.brandType,
                    startTime: data.field.startTime,
                    endTime: data.field.endTime,
                } //设定异步数据接口的额外参数
            });

            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {
                layer.open({
                    title: '新增',
                    type: 2,
                    shadeClose: true,
                    offset: '150px',
                    area: ['550px', '650px'],
                    content: '../page/table/addstatisticsall.html'
                });
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'empEdit':
                    layer.open({
                        title: '修改',
                        type: 2,
                        shadeClose: true,
                        offset: '150px',
                        area: ['550px', '650px'],
                        content: '../page/table/editempstatisticsall.html',
                        success: function (layero, index) {
                            $("#id", layero.find("iframe")[0].contentWindow.document).attr("value", data.id);
                            $("#nickname", layero.find("iframe")[0].contentWindow.document).attr("value", data.nickname);
                            $("#cluesCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.cluesCount);
                            $("#addFanCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.addFanCount);
                            $("#addFanFailCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.addFanFailCount);
                            $("#completeDate", layero.find("iframe")[0].contentWindow.document).attr("value", data.completeDate);
                            $("#phoneCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.phoneCount);
                            $("#phoneNotWxCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.phoneNotWxCount);
                            $("#phoneSuccessCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.phoneSuccessCount);
                            $("#remark", layero.find("iframe")[0].contentWindow.document).attr("value", data.remark);
                        }
                    });
                    break;
                    case 'supEdit':
                    layer.open({
                        title: '修改',
                        type: 2,
                        shadeClose: true,
                        offset: '150px',
                        area: ['550px', '650px'],
                        content: '../page/table/editempstatisticsall.html',
                        success: function (layero, index) {
                            $("#id", layero.find("iframe")[0].contentWindow.document).attr("value", data.id);
                            $("#nickname", layero.find("iframe")[0].contentWindow.document).attr("value", data.nickname);
                            $("#cluesCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.cluesCount);
                            $("#addFanCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.addFanCount);
                            $("#addFanFailCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.addFanFailCount);
                            $("#completeDate", layero.find("iframe")[0].contentWindow.document).attr("value", data.completeDate);
                            $("#phoneCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.phoneCount);
                            $("#phoneNotWxCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.phoneNotWxCount);
                            $("#phoneSuccessCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.phoneSuccessCount);
                            $("#orderSuccessCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.orderSuccessCount);
                            $("#remark", layero.find("iframe")[0].contentWindow.document).attr("value", data.remark);
                        }
                    });
                    break;
                case 'recEdit':
                    layer.open({
                        title: '成单数修改',
                        type: 2,
                        shadeClose: true,
                        offset: '150px',
                        area: ['550px', '300px'],
                        content: '../page/table/editrecstatisticsall.html',
                        success: function (layero, index) {
                            $("#id", layero.find("iframe")[0].contentWindow.document).attr("value", data.id);
                            $("#nickname", layero.find("iframe")[0].contentWindow.document).attr("value", data.nickname);
                            $("#completeDate", layero.find("iframe")[0].contentWindow.document).attr("value", data.completeDate);
                            $("#orderSuccessCount", layero.find("iframe")[0].contentWindow.document).attr("value", data.orderSuccessCount);
                        }
                    });
                    break;
            }
        });

    });
</script>

</body>
</html>
